package cn.tinlan.shopcoin.app.mine.set;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallClazzProxy;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;
import com.zhouyou.http.subsciber.BaseSubscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.Utils.SPUtils;
import cn.tinlan.shopcoin.Utils.TimeUtil;
import cn.tinlan.shopcoin.Utils.ToastUtils;
import cn.tinlan.shopcoin.app.gooddetail.adapter.GoodDetailListAdapter;
import cn.tinlan.shopcoin.app.mine.set.adapter.MoneyHistoryAdapter;
import cn.tinlan.shopcoin.app.mine.set.model.MoneyHistoryItemModel;
import cn.tinlan.shopcoin.base.BaseActivity;
import cn.tinlan.shopcoin.base.CustomApiResult;
import cn.tinlan.shopcoin.widget.category.Constants;
import io.reactivex.Observable;

/**
 * Created by Mortimer on 2018/12/20. 15:28
 * 佛祖🙏，永无bug。
 */
public class MineMoneyHistoryListActivity extends BaseActivity {
    @BindView(R.id.money_history_title)
    TitleBar historyListTitleBar;
    @BindView(R.id.money_history_refresh)
    SmartRefreshLayout historyListRefresh;
    @BindView(R.id.money_history_rv)
    RecyclerView historyListRV;


    private int page = 1;
    private List<MoneyHistoryItemModel>  historyItemModelList;
    private MoneyHistoryAdapter moneyHistoryAdapter;
    private int history_type = 1;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_money_historylist;
    }


    @Override
    public void initView() {
        super.initView();

        drawableDefaultBar();

        initTitleBar();
        initSmartRefresh();
        initHistoryListRView();

//        Log.d("shijiantuo", TimeUtil.timeStamp2Date(1545894385,null));
    }



    private void initTitleBar() {

        String title = getIntent().getStringExtra("title");
        if (title.equals("recharge")) {
            historyListTitleBar.setTitle("充值记录");
            history_type = 1;
        }else if (title.equals("withdraw")){
            historyListTitleBar.setTitle("提现记录");
            history_type = 2;
        }else {
            historyListTitleBar.setTitle("余额变更记录");
            history_type = 3;
        }

        historyListTitleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
    }

    private void initSmartRefresh() {
        historyListRefresh.setRefreshHeader(new ClassicsHeader(this));
        historyListRefresh.setRefreshFooter(new ClassicsFooter(this));
        historyListRefresh.setEnableScrollContentWhenLoaded(true);//是否在加载完成时滚动列表显示新的内容
        historyListRefresh.setEnableFooterFollowWhenLoadFinished(true);
        historyListRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getMoneyHistoryData(page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getMoneyHistoryData(page);
            }
        });
    }

    private void initHistoryListRView() {
        if (moneyHistoryAdapter == null) {
            LinearLayoutManager llm = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) ;
            historyListRV.setLayoutManager(llm);
            historyListRV.setNestedScrollingEnabled(false);
            historyListRV.setHasFixedSize(true);
            historyItemModelList = new ArrayList<>();
            int layoutItem = 0;
            if (history_type == 3) {
                layoutItem = R.layout.view_moneychange_item;
            }else {
                layoutItem = R.layout.view_moneyhistory_item;
            }
            moneyHistoryAdapter = new MoneyHistoryAdapter(layoutItem,history_type,historyItemModelList);
            historyListRV.setAdapter(moneyHistoryAdapter);
        }else {
            moneyHistoryAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void initData() {
        super.initData();
        page = 1;
        getMoneyHistoryData(page);
    }


    private void getMoneyHistoryData(final int page1) {

        HttpParams params = new HttpParams();
        params.put("user_id", SPUtils.getUserID(MineMoneyHistoryListActivity.this));
        params.put("page",page1+"");

        String url = "";
        if (history_type == 1){
            params.put("type",history_type+"");
            url = Constants.UserMoneyRechargeHistory;
        }else if (history_type == 2){
            url = Constants.UserMoneyWithrawHistory;
        }else {
//            params.put("type","");
            url = Constants.UserMoneyRechargeHistory;
        }

        Observable<List<MoneyHistoryItemModel>> observable = EasyHttp.get(url)
                .params(params)
                .execute(new CallClazzProxy<CustomApiResult<List<MoneyHistoryItemModel>>, List<MoneyHistoryItemModel>>(new TypeToken<List<MoneyHistoryItemModel>>() {}.getType()) {
                });

        observable.subscribe(new BaseSubscriber<List<MoneyHistoryItemModel>>() {
            @Override
            public void onError(ApiException e) {

                if (page > 1){
                    page--;
                }else {
                    page = 1;
                }

                ToastUtils.show("数据请求失败");

            }

            @Override
            public void onNext(List<MoneyHistoryItemModel> MoneyHistoryItemModels) {
                super.onNext(MoneyHistoryItemModels);
                historyListRefresh.finishRefresh();
                historyListRefresh.finishLoadMore();
//                if (MoneyHistoryItemModels.size() < 1){
//                    ToastUtils.show("数据暂无");
//                    return;
//                }

                if (page1 == 1){
                    historyItemModelList = MoneyHistoryItemModels;
                    moneyHistoryAdapter.setNewData(historyItemModelList);

//                    mShopListAdapter.notifyDataSetChanged();
                }else {
                    moneyHistoryAdapter.addData(MoneyHistoryItemModels);
                    if (MoneyHistoryItemModels.size() < 16) {
                        if (MoneyHistoryItemModels.size() < 1 ){
                            if (page > 1){
                                page--;
                            }else {
                                page = 1;
                            }
                        }
                        historyListRefresh.finishLoadMoreWithNoMoreData();  //全部加载完成,没有数据了调用此方法
                    }else {
//                        historyListRefresh.finishLoadMore();      //加载完成
                    }

//
                }

            }
        });
    }
}
